package com.ruozedata.hadoop;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
// import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.IOException;

public class AccessDriver {
    public static void main(String[] args) throws Exception {
        String input = "data/access.log";
        String output = "out1";


        final Configuration configuration = new Configuration();

        // 1、获取Job对象
        final Job job = Job.getInstance(configuration);

        // 删除输出文件夹
        // FileUtils.deleteTarget(configuration,output);

        // 2、设置class
        job.setJarByClass(AccessDriver.class);

        // 3、设置Mapper和Reducer
        job.setMapperClass(MyMapper.class);
        job.setReducerClass(MyReducer.class);

        // 4、设置Mapper阶段输出数据的key和value的类型
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(Access.class);

        // 5、设置Reducer阶段输出数据的key和value的类型
        job.setOutputKeyClass(NullWritable.class);
        job.setOutputValueClass(Access.class);

        // 6、设置输入和输出数据的路径
        FileInputFormat.setInputPaths(job, new Path(input));
        FileOutputFormat.setOutputPath(job, new Path(output));

        // 7、提交Job
        final boolean result = job.waitForCompletion(true);
        System.exit(result ? 0 : 1);
    }

    public static class MyMapper extends Mapper<LongWritable, Text, Text, Access> {
        @Override
        protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
            String[] splits = value.toString().split("\t");
            final String phone = splits[1];  // 第二个字段：手机号
            long up = Long.parseLong(splits[splits.length-3]);  //倒数第三个字段：上行流量
            long down = Long.parseLong(splits[splits.length-2]); // 倒数第二个字段：下行流量

            final Access access = new Access();
            access.setPhone(phone);
            access.setUp(up);
            access.setDown(down);
            access.setSum(up + down);

            context.write(new Text(phone), access);
        }
    }

    public static class MyReducer extends Reducer<Text, Access, NullWritable, Access> {
        @Override
        protected void reduce(Text phone, Iterable<Access> values, Context context) throws IOException, InterruptedException {
            long ups = 0;
            long downs = 0;

            for (Access access : values) {
                ups += access.getUp();
                downs += access.getDown();
            }

            final Access access = new Access();
            access.setPhone(phone.toString());
            access.setUp(ups);
            access.setDown(downs);
            access.setSum(ups + downs);

            context.write(NullWritable.get(), access);
        }
    }
}
